/*
**  Copyright (c) 2014 Michael Liu(michael.liu.point@gmail.com).
**
**  Project: Gateway Unified Management Platform
**  File:      sadev.h
**  Author:  Michael
**  Date:    06/24/2014
**
**  Purpose:
**    system devices management defines.
*/

#ifndef __SYSTEM_DEIVCE_MGR_H__
#define __SYSTEM_DEIVCE_MGR_H__

#ifdef __cplusplus
extern "C" {
#endif				/* __cplusplus */

/* Include files. */

#include "common.h"
#include "cmrc.h"
#include "list.h"

/* Macro constant definitions. */

#define SA_DEV_NAMESIZE					(16)
#define SA_DEV_REF_MAX					(8)

#define VERSION_STR_SIZE				(32)
#define SYS_ADMIN_NTP_SERVER_URL_SIZE	(128)
#define IH_NMLEN						(32-4)	/* Image Name Length */
#define IH_MAGIC						0x27051956	/* Image Magic Number */
#define MTD_IMAGE						"/dev/mtd4"

#define SWAP_LONG(x) \
	((__u32)( \
		(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
		(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
		(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
		(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))

#define     NTOHL(a)	SWAP_LONG(a)
#define     HTONL(a)	SWAP_LONG(a)

/* Type definitions. */

	enum {
		eSA_DEV_OPT_INIT,
		eSA_DEV_OPT_CLEAN,
	} esadev_opt;

	enum {
		eGPIO_OPT_INIT = eSA_DEV_OPT_INIT,
		eGPIO_OPT_CLEAN = eSA_DEV_OPT_CLEAN,
		eGPIO_OPT_SET_DIR,
		eGPIO_OPT_READ,
		eGPIO_OPT_WRITE,
		eGPIO_OPT_ENABLE_IRQ,
		eGPIO_OPT_DISABLE_IRQ,
		eGPIO_OPT_REG_PROCCESS,
		eGPIO_OPT_GET_INTP,
		eGPIO_OPT_SET_LED,
	} egpio_opt;

	enum {
		eLED_OPT_INIT = eSA_DEV_OPT_INIT,
		eLED_OPT_CLEAN = eSA_DEV_OPT_CLEAN,
		eLED_OPT_SET_POWER_SAVE,
		eLED_OPT_GET_POWER_SAVE,
		eLED_OPT_SET_24G_SIGNAL_NONE,
		eLED_OPT_SET_24G_SIGNAL_LOW,
		eLED_OPT_SET_24G_SIGNAL_NORMAL,
		eLED_OPT_SET_24G_SIGNAL_HIGH,
		eLED_OPT_SET_24G_SCAN,
		eLED_OPT_SET_24G_WPS_PROCESS,
		eLED_OPT_SET_24G_WPS_SUCCESS,
		eLED_OPT_SET_24G_WPS_FAIL,
		eLED_OPT_SET_24G_WPS_OVERLAP,
		eLED_OPT_SET_5G_SIGNAL_NONE,
		eLED_OPT_SET_5G_SIGNAL_LOW,
		eLED_OPT_SET_5G_SIGNAL_NORMAL,
		eLED_OPT_SET_5G_SIGNAL_HIGH,
		eLED_OPT_SET_5G_SCAN,
		eLED_OPT_SET_5G_WPS_PROCESS,
		eLED_OPT_SET_5G_WPS_SUCCESS,
		eLED_OPT_SET_5G_WPS_FAIL,
		eLED_OPT_SET_5G_WPS_OVERLAP,
	} eled_opt;

	enum {
		eSIGNAL_LED_LOW,
		eSIGNAL_LED_NORMAL,
		eSIGNAL_LED_HIGH,
		eSIGNAL_LED_MAX,
	} esignal_led;

	enum {
		eBUTTON_WPS,
		eBUTTON_RESET,
		eBUTTON_MAX,
	} ebutton;

	enum {
		eMODE_SW_OPT_INIT = eSA_DEV_OPT_INIT,
		eMODE_SW_OPT_CLEAN = eSA_DEV_OPT_CLEAN,
		eMODE_SW_OPT_CHECK,
	} emode_sw_opt;

	enum {
		eBUTTON_OPT_INIT = eSA_DEV_OPT_INIT,
		eBUTTON_OPT_CLEAN = eSA_DEV_OPT_CLEAN,
		eBUUTON_OPT_CHECK,
	} ebutton_opt;

	enum {
		eBUTTON_INPT_NONE,
		eBUTTON_INPT_SHORT_PRESS,
		eBUTTON_INPT_LONG_PRESS,
	} ebutton_inpt;

	enum {
		eGPIO_DIR_IN,
		eGPIO_DIR_OUT,
	} egpio_dir;

	enum {
		eGPIO_NO_00,
		eGPIO_NO_01,
		eGPIO_NO_02,
		eGPIO_NO_03,
		eGPIO_NO_04,
		eGPIO_NO_05,
		eGPIO_NO_06,
		eGPIO_NO_07,
		eGPIO_NO_08,
		eGPIO_NO_09,
		eGPIO_NO_10,
		eGPIO_NO_11,
		eGPIO_NO_12,
		eGPIO_NO_13,
		eGPIO_NO_14,
		eGPIO_NO_15,
		eGPIO_NO_16,
		eGPIO_NO_17,
		eGPIO_NO_18,
		eGPIO_NO_19,
		eGPIO_NO_20,
		eGPIO_NO_21,
		eGPIO_NO_22,
		eGPIO_NO_23,
		eGPIO_NO_24,
		eGPIO_NO_25,
		eGPIO_NO_26,
		eGPIO_NO_27,
		eGPIO_NO_28,
		eGPIO_NO_29,
		eGPIO_NO_30,
		eGPIO_NO_31,
		eGPIO_NO_32,
		eGPIO_NO_33,
		eGPIO_NO_34,
		eGPIO_NO_35,
		eGPIO_NO_36,
		eGPIO_NO_37,
		eGPIO_NO_38,
		eGPIO_NO_39,
		eGPIO_NO_40,
		eGPIO_NO_41,
		eGPIO_NO_42,
		eGPIO_NO_43,
		eGPIO_NO_44,
		eGPIO_NO_45,
		eGPIO_NO_46,
		eGPIO_NO_47,
		eGPIO_NO_48,
		eGPIO_NO_49,
		eGPIO_NO_50,
		eGPIO_NO_51,
		eGPIO_NO_52,
		eGPIO_NO_53,
		eGPIO_NO_54,
		eGPIO_NO_55,
		eGPIO_NO_56,
		eGPIO_NO_57,
		eGPIO_NO_58,
		eGPIO_NO_59,
		eGPIO_NO_60,
		eGPIO_NO_61,
		eGPIO_NO_62,
		eGPIO_NO_63,
		eGPIO_NO_64,
		eGPIO_NO_65,
		eGPIO_NO_66,
		eGPIO_NO_67,
		eGPIO_NO_68,
		eGPIO_NO_69,
		eGPIO_NO_70,
		eGPIO_NO_71,
		eGPIO_NO_72,
		eGPIO_NO_73,
		eGPIO_NO_74,
		eGPIO_NO_75,
		eGPIO_NO_76,
		eGPIO_NO_77,
		eGPIO_NO_78,
		eGPIO_NO_79,
		eGPIO_NO_80,
		eGPIO_NO_81,
		eGPIO_NO_82,
		eGPIO_NO_83,
		eGPIO_NO_84,
		eGPIO_NO_85,
		eGPIO_NO_86,
		eGPIO_NO_87,
		eGPIO_NO_88,
		eGPIO_NO_89,
		eGPIO_NO_90,
		eGPIO_NO_91,
		eGPIO_NO_92,
		eGPIO_NO_93,
		eGPIO_NO_94,
		eGPIO_NO_95,
		eGPIO_NO_96,
	} egpio_no;

	typedef struct led_cfg tled_cfg;
	struct led_cfg {
		unsigned int on;
		unsigned int off;
		unsigned int blinks;
		unsigned int rests;
		unsigned int times;
		unsigned int on_delay;
	};

	typedef struct gpio_opt_cfg tgpio_opt_cfg;
	struct gpio_opt_cfg {
		unsigned char gpio;
		union {
			unsigned char dir;
			unsigned char intp;
			unsigned long rw_value;
			unsigned long pid;
			tled_cfg led;
		};
	};

	typedef struct led_opt_cfg tled_opt_cfg;
	struct led_opt_cfg {
		unsigned char power_save;
	};

	typedef struct sa_dev tsa_dev;
	struct sa_dev {
		char name[SA_DEV_NAMESIZE];
		unsigned char prepare;	/* eBOOL_TURE or eBOOL_FALSE */
		unsigned char init;	/* eBOOL_TURE or eBOOL_FALSE */
		tsa_dev *based_on_dev;	/* based on dev pointer. */
		int dev_fd;
		struct list_head list;
		unsigned char refs_cnt;
		void *refs[SA_DEV_REF_MAX];	/* reference array. */
		void *priv;
		 e_ret(*add_ref) (tsa_dev * me, void *ref);
		 e_ret(*del_ref) (tsa_dev * me, void *ref);
		 e_ret(*opt) (tsa_dev * sa_dev, unsigned char opt, void *cfg);
	};

	typedef struct version tversion;
	struct version {
		char model[VERSION_STR_SIZE];
		char version[VERSION_STR_SIZE];
		t_date_time rel_time;
	};

	typedef struct sys_admin tsys_admin;
	struct sys_admin {
		tversion fw_info;
		tversion fota_info;
		char ntp_server[SYS_ADMIN_NTP_SERVER_URL_SIZE];
		struct list_head all_devs;
	};

	typedef struct image_header {
		unsigned int ih_magic;	/* Image Header Magic Number    */
		unsigned int ih_hcrc;	/* Image Header CRC Checksum    */
		unsigned int ih_time;	/* Image Creation Timestamp     */
		unsigned int ih_size;	/* Image Data Size              */
		unsigned int ih_load;	/* Data  Load  Address          */
		unsigned int ih_ep;	/* Entry Point Address          */
		unsigned int ih_dcrc;	/* Image Data CRC Checksum      */
		unsigned char ih_os;	/* Operating System             */
		unsigned char ih_arch;	/* CPU architecture             */
		unsigned char ih_type;	/* Image Type                   */
		unsigned char ih_comp;	/* Compression Type             */
		unsigned char ih_name[IH_NMLEN];	/* Image Name           */
		unsigned int ih_ksz;	/* Kernel Part Size             */
	} image_header_t;

/* External function declarations. */

	extern e_ret sa_dev_prepare(tsa_dev * me, e_ret(*opt) (tsa_dev * sa_dev, unsigned char opt, void *cfg));

/* Macro API definitions. */

/* Global variable declarations. */

#ifdef __cplusplus
}				/* extern "C" */
#endif				/* __cplusplus */
#endif				/* __SYSTEM_DEIVCE_MGR_H__ */
